﻿using ImTools;
using MyToDo.Common.Models;
using MyToDo.Common.Prism;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyToDo.ViewModels
{
    public class MainViewModel : BindableBase//继承BindableBase可以动态更新
    {

        public MainViewModel(IRegionManager regionManager)//引入regionManager路由管理
        {
            menuBars = new ObservableCollection<MenuBar>();
            CreateMenuBar();
            NavigateCommand = new DelegateCommand<MenuBar>(Navigate);
            this.regionManager = regionManager;
            //添加前进后退按钮事件
            GoBackBtn = new DelegateCommand(() =>
            {
                if (journal!=null && journal.CanGoBack)
                {
                    journal.GoBack();
                }
            });
            GoForwardBtn = new DelegateCommand(() =>
            {
                if (journal != null && journal.CanGoForward)
                {
                    journal.GoForward();
                }
            });
        }


        #region  代码折叠
        /// <summary>
        /// 路由导航功能
        /// </summary>
        /// <param name="obj">路由菜单</param>
        private void Navigate(MenuBar obj)
        {
            //加入判断，如果是空对象或者命名空间为空则跳出
            if (obj == null || string.IsNullOrEmpty(obj.NameSpace))
            {
                return;
            }
            regionManager.Regions[PrismManager.MainViewRegionName].RequestNavigate(obj.NameSpace, back =>
            {
                //使用回调函数，添加路由日志
                journal = back.Context.NavigationService.Journal;
            });



        }
        #endregion
        /// <summary>
        /// 用于导航
        /// </summary>
        public DelegateCommand<MenuBar> NavigateCommand { get; set; }
        /// <summary>
        /// 返回按钮
        /// </summary>
        public DelegateCommand GoBackBtn { get; set; }
        /// <summary>
        /// 前进按钮
        /// </summary>
        public DelegateCommand GoForwardBtn { get; set; }

        /// <summary>
        /// 引入Prism路由管理
        /// </summary>
        private readonly IRegionManager regionManager;
        /// <summary>
        /// Prism导航日志，路由前进或者后退
        /// </summary>
        private IRegionNavigationJournal journal;

        private ObservableCollection<MenuBar> menuBars;

        public ObservableCollection<MenuBar> MenuBars
        {
            get { return menuBars; }
            set { menuBars = value; RaisePropertyChanged(); }
        } //动态更新列表

        void CreateMenuBar()
        {
            MenuBars.Add(new MenuBar { Icon = "Home", Title = "首页", NameSpace = "IndexView" });
            MenuBars.Add(new MenuBar { Icon = "Notebook", Title = "代办事项", NameSpace = "ToDoView" });
            MenuBars.Add(new MenuBar { Icon = "NotebookEdit", Title = "备忘录", NameSpace = "MenuView" });
            MenuBars.Add(new MenuBar { Icon = "CogOutline", Title = "设置", NameSpace = "SettingView" });
        }
    }
}
